import SockJS from "sockjs-client/dist/sockjs.min.js";
import Stomp from "stompjs";
import { BASE_URL } from "@/service/request/config.js";

class Socket {
    instance;
    stompClient;
    opt;
    isConnect = false;
    constructor() {
        this.instance = new SockJS(BASE_URL + "/qianduanToHouduan?eceibstoken=" + localStorage.getItem("token"));
        this.stompClient = Stomp.over(this.instance);
    }
    connect(url, callBack, opt) {
        this.opt = opt;
        this.stompClient.connect(
            { eceibstoken: localStorage.getItem("token") },
            () => {
                this.isConnect = true;
                this.stompClient.subscribe(url, (res) => callBack(JSON.parse(res.body), this.opt), { eceibstoken: localStorage.getItem("token") });
            },
            (err) => {
                this.isConnect = false;
                console.log(err);
            }
        );
    }
    disconnect() {
        if (this.stompClient && this.isConnect) {
            this.stompClient.disconnect();
        }
    }
}

export default Socket;
